package cn.itcast.aspect;


import cn.itcast.mapper.OperateLogMapper;
import cn.itcast.pojo.OperateLog;
import cn.itcast.utils.TempMap;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;

@Aspect
@Component
public class recordLogAdvice {

    @Autowired
    private OperateLogMapper operateLogMapper;

    @Around("@annotation(cn.itcast.aspect.LogOperation)")
    public Object recordLog(ProceedingJoinPoint pjp) throws Throwable {

        Signature signature = pjp.getSignature();
        String className = signature.getDeclaringTypeName();

        //String className1 = signature.getClass().getName();
        //cn.itcast.service.impl.EmpServiceImpl
        System.out.println("signature.getDeclaringTypeName()" + className);
        System.out.println("===========================第二========================");
        //org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint$MethodSignatureImpl
        //System.out.println("signature.getClass().getName()" + className1);
        String methodName = signature.getName();// 获取方法名
        LocalDateTime nowTime = LocalDateTime.now();// 获取当前时间
        String params = Arrays.toString(pjp.getArgs());// 获取参数
        String returnValue = null;
        long time = 0;
        try {
            long start = System.currentTimeMillis();
            Object result = pjp.proceed();
            returnValue = result.toString();// 获取返回值
            long end = System.currentTimeMillis();
            time = end - start;// 获取执行时间
            return result;
        } finally {
            OperateLog operateLog = new OperateLog(null, (Integer) TempMap.get(), nowTime, className, methodName, params, returnValue, time, null);
            operateLogMapper.insert(operateLog);
            System.out.println("我是recordLogAdvice里面的aop====第三");
        }
    }
}
